<template>
  <div class="root">
    <el-table :data="tableData" style="width: 100%" @cell-dblclick="tableEdit">
      <el-table-column
        width="100"
        prop="username"
        label="姓名"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        width="120"
        prop="telephone"
        label="手机号"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="notice"
        label="需要通知的信息（双击输入即保存！！）"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        width="120"
        prop="isSignin"
        label="状态"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isSignin">已到场</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="params.pageSize"
      @current-change="pageChange"
      @prev-click="pageChange"
      @next-click="pageChange"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      tableData: []
    };
  },
  mounted() {
    console.log(this);
    this.getTableData();
  },
  methods: {
    getTableData() {
      let that = this;
      this.req({
        url: `/admin/member/list?activityId=${this.$route.query.activityId}&page=${this.params.page}&pageSize=${this.params.pageSize}`,
        method: "POST"
      }).then(
        res => {
          that.total = res.data.total;
          that.tableData = res.data.list;
        },
        err => {
          console.log("err :", err);
        }
      );
    },
    pageChange(page) {
      this.params.page = page;
      this.getTableData();
    },
    // 双击表格修改数据
    tableEdit(row, column, cell, event) {
      const that = this;
      if (column.label == "需要通知的信息（双击输入即保存！！）") {
        var beforeVal = event.target.textContent;
        event.target.innerHTML = "";
        let str = `<div class='cell'>
            <div class='el-input'>
              <input type='text' placeholder='餐桌位，酒店入住房号等' class='el-input__inner'>
            </div>
        </div>`;
        cell.innerHTML = str;
        //  获取双击后生成的input  根据层级嵌套会有所变化
        let cellInput = cell.children[0].children[0].children[0];
        cellInput.value = beforeVal;
        cellInput.focus(); // input自动聚焦
        // 失去焦点后  将input移除
        cellInput.onblur = function() {
          let onblurCont = `<div class='cell'>${cellInput.value}</div>`;
          cell.innerHTML = onblurCont; // 换成原有的显示内容
          // 调用axios接口
          console.log(cellInput.value, "===cellInput.value");
          that
            .req({
              url: `/admin/member/edit_notice?memberId=${row.memberId}&notice=${cellInput.value}`,
              method: "POST"
            })
            .then(
              res => {
                if (res.code === 200) {
                  that.$message({
                    type: "success",
                    message: res.msg
                  });
                }
              },
              err => {
                console.log("err :", err);
              }
            );
        };
      }
    }
  }
};
</script>
<style>
.pagination {
  margin-top: 20px;
  margin-right: 50px;
  float: right;
}
</style>
